package cn.jcby.app.lebu.utils;

import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;

/**
 * @author LHT
 * @date 2022-05-20
 */
public class LocalDateUtils extends DateUtils {

    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss" ;

    public static Date get2MonthBefore(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime() ;
    }
    public static String format(Date date,String format){
        return new SimpleDateFormat(format).format(date) ;
    }

    public static String format(Date date){
        return format(date,YYYY_MM_DD_HH_MM_SS) ;
    }

    public static Integer getAge(LocalDate birthday) {
        LocalDate currentDate = LocalDate.now(); // 获取当前日期

        Period age = Period.between(birthday, currentDate); // 计算年龄差距
        int years = age.getYears(); // 获取年份差距
        return years;
    }

    public static Date thisMonthDay() {
        Date thisMonth = new Date() ;
        thisMonth = DateUtils.truncate(thisMonth, Calendar.MONTH) ;
        return thisMonth ;
    }

    public static void main(String[] args) {
        System.out.println(thisMonthDay());
    }
}
